<?php
// +----------------------------------------------------------------------
// | [RhaPHP System] Copyright (c) 2017 http://www.rhaphp.com/
// +----------------------------------------------------------------------
// | [RhaPHP] 并不是自由软件,你可免费使用,未经许可不能去掉RhaPHP相关版权
// +----------------------------------------------------------------------
// | Author: Geeson <qimengkeji@vip.qq.com>
// +----------------------------------------------------------------------


namespace app\common\controller;
\think\Loader::import('controller/Jump', TRAIT_PATH, EXT);

use app\common\model\Addons;

class Addon extends Common
{
    use \traits\controller\Jump;
    public $mpInfo;
    private $addonName;
    private $addonController;
    private $addonAction;
    public $addonInfoByDb;
    public $addonInfoByFile;
    public $getAaddonConfigByMp;
    public $addonRoot;

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->mpInfo = getMpInfo();
        $addonRule = session('addonRule');
        $this->addonName = $addonRule['addon'];
        $this->addonController = $addonRule['col'];
        $this->addonAction = $addonRule['act'];
        $model = new Addons();
        $this->addonInfoByFile = $model->getAddonByFile($this->addonName);
        $this->addonInfoByDb = $model->getAddonByDb($this->addonName);
        $this->getAaddonConfigByMp = $model->getAaddonConfigByMp($this->addonName, $this->mid);
        $this->addonRoot = ADDON_PATH . $this->addonName . '/';
        session('addonName', $this->addonName);
        session('mid', $this->mid);

    }

    public function fetch($template = '', $vars = [], $replace = [], $config = [])
    {
        if ($template == null) {
            $template = $this->addonAction;
        }
        if ($template == 'default') {
            $template = APP_PATH . 'common/view/default.' . config('template.view_suffix');
            echo parent::fetch($template, $vars, $replace, $config); // TODO: Change the autogenerated stub
        } else {
            $template = ADDON_PATH . $this->addonName . '/view/' . strtolower($this->addonController) . '/' . $template . '.' . config('template.view_suffix');
            $config['view_path'] = ADDON_PATH . $this->addonName . '/view/';
            echo parent::fetch($template, $vars, $replace, $config); // TODO: Change the autogenerated stub
        }

    }

    public function assign($name, $value = '')
    {
        parent::assign($name, $value); // TODO: Change the autogenerated stub
    }

    public function getAdonnURL($url = '')
    {
        $node = '';
        if ($url == '') {
            $node = $this->addonName . DS . $this->addonController . DS . $this->addonAction;
        } else {
            $nodeArr = array_values(array_filter(explode('/', $url)));
            switch (count($nodeArr)) {
                case 1:
                    $node = $this->addonName . DS . $this->addonController . DS . $nodeArr[0];
                    break;
                case 2:
                    $node = $this->addonName . DS . $nodeArr[0] . DS . $nodeArr[1];
                    break;
                case 3:
                    $node = $node = $nodeArr[0] . DS . $nodeArr[1] . DS . $nodeArr[2];
                    break;
            }
        }
        $url = \think\Url::build(ADDON_ROUTE . $node, ['mid' => $this->mid]);
        return $url = str_replace('.' . config('template.view_suffix'), '', $url);;

    }


}